<template>
  <div>
    <el-form :model="queryParam" :inline=true style="display: flex">
      <el-form-item label="事件code">
        <el-input v-model="queryParam.eventCode" style="width:160px"/>
      </el-form-item>
      <el-form-item label="步骤code">
        <el-input v-model="queryParam.stepCode" style="width:160px"/>
      </el-form-item>
      <el-form-item label="接口mockcode">
        <el-input v-model="queryParam.mockCode" style="width:160px"/>
      </el-form-item>
      <el-form-item label="请求参数">
        <el-input v-model="queryParam.request" style="width:160px"/>
      </el-form-item>
      <el-form-item label="响应参数">
        <el-input v-model="queryParam.response" style="width:160px"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="queryPage">搜索</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="danger" @click="refreshConf">刷新配置</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData" style="width: 100%" :row-class-name="tableRowClassName">
      <el-table-column prop="id" label="序号" width="80"></el-table-column>
      <el-table-column label="事件code与名" width="130">
        <template slot-scope="scope">
          <div class="typeEnter">code: {{ scope.row.eventCode }}</div>
          <div class="typeEnter">名称: {{ scope.row.eventName }}</div>
        </template>
      </el-table-column>
      <el-table-column label="步骤code与名" width="130">
        <template slot-scope="scope">
          <div class="typeEnter">code: {{ scope.row.stepCode }}</div>
          <div class="typeEnter">名称: {{ scope.row.stepName }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="mockCode" label="mockcode" width="100"></el-table-column>
      <el-table-column label="开关" width="80">
        <template slot-scope="scope">
          <div class="typeEnter">生产否: {{ scope.row.openProducer }}</div>
          <div class="typeEnter">消费否: {{ scope.row.openConsumer }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="50"></el-table-column>
      <el-table-column label="contentType" width="110">
        <template slot-scope="scope">
          <div class="typeEnter">请求格式: {{ scope.row.reqContentType }}</div>
          <div class="typeEnter">响应格式: {{ scope.row.respContentType }}</div>
        </template>
      </el-table-column>
      <el-table-column label="http配置">
        <template slot-scope="scope">
          <div class="typeEnter">请求地址: {{ scope.row.apiUrl }}</div>
          <div class="typeEnter">参数格式: {{ scope.row.mockParam }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="pageTotal>0"
      :total="pageTotal"
      :page.sync="queryParam.pageNum"
      :limit.sync="queryParam.pageSize"
      @pagination="queryPage"
    />
  </div>
</template>

<script>
import {ellipsis, parseTime} from '@/utils'

export default {
  data() {
    return {
      // 查询参数
      pageTotal: 0,
      queryParam: {
        eventCode: '',
        stepCode: '',
        mockCode: '',
        request: '',
        response: '',
        pageNum: 1,
        pageSize: 10
      },
      tableData: [],
      colorkeys: {}
    }
  },
  mounted() {
    this.pageList()
  },
  methods: {
    ellipsis,
    parseTime,
    queryPage() {
      var _this = this
      _this.pageList()
    },
    pageList() {
      let _this = this
      this.$api.trigerConfPageList(_this.queryParam).then(res => {
        if (res.code === '00000') {
          _this.tableData = res.data.pageList
          _this.pageTotal = res.data.pageTotal

          _this.tableData.forEach(e => this.colorkeys[e.eventCode] = 1)
        } else {
          _this.$message(res.msg)
        }
      }).catch(e => {
        console.log('接口访问异常!')
      })
    },
    //只接收一个参数,内部名字是映射
    tableRowClassName({row, rowIndex}) {
      let colorsize = 5
      let keyArr = Object.getOwnPropertyNames(this.colorkeys)
      let len = keyArr.length
      for (let i = 0; i < len; i++) {
        this.colorkeys[keyArr[i]] = i % colorsize
      }

      let n = this.colorkeys[row['eventCode']];
      if (n < colorsize) {
        return "row-color-" + n;
      } else {
        return '';
      }
    },
    refreshConf() {
      let _this = this
      this.$api.mockConfRefresh().then(res => {
        if (res.code === '00000') {
          _this.$message("配置刷新成功")
        } else {
          _this.$message(res.msg)
        }
      }).catch(e => {
        console.log('接口访问异常!')
      })
    }
  },
  filters: {
    ellipsis: (str) => ellipsis(str, 300)
  }
}
</script>

<style>
.el-table .row-color-0 {
  background: #8cc5ff;
}

.el-table .row-color-1 {
  background: #cf9236;
}

.el-table .row-color-2 {
  background: #85ce61;
}

.el-table .row-color-3 {
  background: #f78989;
}

.el-table .row-color-4 {
  background: #ebb563;
}
</style>
